﻿# SharePoint 2007 PowerShell Module
# http://powershell.codeplex.com

$global:SPFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local

Function Get-SPFarm
{
	return $SPFarm
}

Function Get-SPServer
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[string] $Identity
	)

	if ($Identity)
	{
		return $SPFarm.Servers | Where-Object {$_.Address -eq $Identity}
	}
	else
	{
		return $SPFarm.Servers
	}
}

Function Get-SPServiceApplication
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[string] $Name
	)

	if ($Name)
	{
		return $SPFarm.Services | Where-Object {$_.Name -ceq $Name}
	}
	else
	{
		return $SPFarm.Services
	}
}

Function Get-SPSearchService
{
	return Get-SPServiceApplication -Name 'OSearch'
}

Function Get-SPWebApplication
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[string] $Identity
	)

	if ($Identity)
	{
		return Get-SPServiceApplication | Where-Object {$_.TypeName -ceq 'Windows SharePoint Services Web Application'} | ForEach-Object {$_.WebApplications | Where-Object {$_.Name -eq $Identity}}
	}
	else
	{
		return Get-SPServiceApplication | Where-Object {$_.TypeName -ceq 'Windows SharePoint Services Web Application'} | ForEach-Object {$_.WebApplications}
	}
}

Function Get-SPSite
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[string] $Identity
	)

	if ($Identity)
	{
		return Get-SPWebApplication | ForEach-Object {$_.Sites | Where-Object {$_.Url -eq $Identity}}
	}
	else
	{
		return Get-SPWebApplication | ForEach-Object {$_.Sites}
	}
}

Function Get-SPWeb
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[Parameter(Mandatory=$true)]
		[string] $Identity
	)

	$ErrorActionPreference = 'SilentlyContinue'
	return Get-SPSite | ForEach-Object {$_.AllWebs | Where-Object {$_.Url -eq $Identity}}
}

Function New-SPListItem
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[Parameter(Mandatory=$true)]
		[string] $Site,
		[Parameter(Mandatory=$true)]
		[string] $List,
		[Parameter(Mandatory=$true)]
		[hashtable] $Hash
	)

	$SPWeb = Get-SPWeb -Identity $Site
	$SPList = $SPWeb.Lists | Where-Object {$_.Title -eq $List}
	$SPListItem = $SPList.Items.Add()
	foreach ($Item in $Hash.GetEnumerator())
	{
		$SPListItem[$Item.Key] = $Item.Value
		$SPListItem.Update()
	}
}